package com.jewelry.service.impl;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.jewelry.dao.IConsumablesLogDAO;
import com.jewelry.service.IConsumablesLogService;
import com.jewelry.service.IConsumablesService;
import com.jewelry.vo.ConsumablesLogVO;
import com.jewelry.vo.ConsumablesVO;

public class ConsumablesLogServiceImpl implements IConsumablesLogService {

	private IConsumablesLogDAO consumablesLogDAO;

	public IConsumablesLogDAO getConsumablesLogDAO() {
		return consumablesLogDAO;
	}

	public void setConsumablesLogDAO(IConsumablesLogDAO consumablesLogDAO) {
		this.consumablesLogDAO = consumablesLogDAO;
	}
	
	private IConsumablesService consumablesService;
	

	public IConsumablesService getConsumablesService() {
		return consumablesService;
	}

	public void setConsumablesService(IConsumablesService consumablesService) {
		this.consumablesService = consumablesService;
	}

	//=================================================================
	
	@Override
	public List<ConsumablesLogVO> getConsumablesLogByPage(ConsumablesLogVO vo) {
		int totalItem = consumablesLogDAO.getConsumablesLogCount(vo);
		vo.getPage().setTotalItem(totalItem);
		return consumablesLogDAO.getConsumablesLogListByPage(vo);
	}

	@Override
	public int getConsumablesLogCount(ConsumablesLogVO vo) {
		return consumablesLogDAO.getConsumablesLogCount(vo);
	}

	//本方法需要处理事务
	@Override
	@Transactional(rollbackFor=Exception.class) 
	public void addConsumablesLog(ConsumablesLogVO consumablesLog) {
		int consumablesid = consumablesLog.getConsumablesid();
		String consumablesName = consumablesService.getConsumablesNameByID(consumablesid);
		consumablesLog.setConsumablesName(consumablesName);
		consumablesLogDAO.addConsumablesLog(consumablesLog);
		//入库
		ConsumablesVO vo = new ConsumablesVO();
		vo.setConsumablesid(consumablesid);
		vo.setQuantity(consumablesLog.getQuantity());
		vo.setConsumablesid(consumablesLog.getConsumablesid());
		if(consumablesLog.getCategory()==2){ //领用
			vo.setQuantity(-vo.getQuantity());
		}
		consumablesService.updateConsumablesQuantity(vo);
	}
	
}
